package com.example.campuslink.viewModel.userOperation

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.campuslink.commen.db.school.SchoolInfoTable
import com.example.campuslink.viewModel.BaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginViewModel : BaseViewModel() {
    private val _showPassword = MutableLiveData<Boolean>(false)
    val showPassword: MutableLiveData<Boolean> = _showPassword
    private val _showRegisterDialog = MutableLiveData<Boolean>(false)
    val showRegisterDialog = _showRegisterDialog
    private val _currentPosition = MutableLiveData<Int>(0)
    val currentPosition = _currentPosition

    // TODO: 测试添加默认学校 
    fun addDefaultSchool() {
        viewModelScope.launch(Dispatchers.IO) {
            var schoolName = "仰恩大学"
            var schoolContent = "民办 本科"
            var schoolInfo = SchoolInfoTable()
            schoolInfo.schoolName = schoolName
            schoolInfo.schoolIntroduce = mutableListOf(schoolContent)
            schoolInfo.province = "福建省"
//            schoolInfo.schoolImage = imgPath
//            schoolInfo.searchData = mutableListOf()
//            schoolInfo.messageList = mutableListOf()
//            schoolInfo.mainInvitationList = mutableListOf()
//            schoolInfo.shoppingInvitationList = mutableListOf()
            if (schoolInfoDao?.getSchoolInfoBySchoolName(schoolName) == null) {
                schoolInfoDao.insertSchoolInfo(schoolInfo)
            } else {
                viewModelScope.launch(Dispatchers.Main) {
//                    Toast.makeText(context, "该学校已存在", Toast.LENGTH_SHORT).show()
                }
            }
            schoolInfo = SchoolInfoTable()
            schoolName = "上海交通大学"
            schoolContent = "本科 双一流"
            schoolInfo.schoolName = schoolName
            schoolInfo.schoolIntroduce = mutableListOf(schoolContent)
            schoolInfo.province = "上海市"
            schoolInfo.schoolImage =
                "https://ts1.tc.mm.bing.net/th/id/R-C.026abfcb67a67894897be5882f3593cc?rik=1T6IHEC%2bXs97lw&riu=http%3a%2f%2fwww.kuaipng.com%2fUploads%2fpic%2fw%2f2022%2f05-30%2f125659%2fwater_125659_698_698_.png&ehk=Qm0AERJXW9Nvj9FF%2bg4QwslpWwu%2bFCEEwunrJBYo%2fXI%3d&risl=&pid=ImgRaw&r=0"
            if (schoolInfoDao?.getSchoolInfoBySchoolName(schoolName) == null) {
                schoolInfoDao.insertSchoolInfo(schoolInfo)
            }
            schoolInfo = SchoolInfoTable()
            schoolName = "北京大学"
            schoolContent = "本科 双一流"
            schoolInfo.schoolName = schoolName
            schoolInfo.schoolIntroduce = mutableListOf(schoolContent)
            schoolInfo.province = "北京市"
            schoolInfo.schoolImage =
                "https://tse1-mm.cn.bing.net/th/id/OIP-C.tNAe2pQXPkUxCU_idmSAQQHaJb?rs=1&pid=ImgDetMain"
            if (schoolInfoDao?.getSchoolInfoBySchoolName(schoolName) == null) {
                schoolInfoDao.insertSchoolInfo(schoolInfo)
            }
        }
    }

    fun setShowPassword(
    ) {
        _showPassword.value = !(_showPassword.value ?: false)
    }

    fun setCurrentPosition(index: Int) {
        _currentPosition.value = index
    }

    fun showRegisterDialog() {
        _showRegisterDialog.value = true
    }

    fun setShowRegisterDialog() {
        _showRegisterDialog.value = false
    }

}